package com.isunland.managesystem.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.Task;
import com.isunland.managesystem.ui.TaskListFragment;
import com.isunland.managesystem.utils.LogUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskListAdapter extends ArrayAdapter<Task> {
  private boolean isBat = false;//是否是多选显示checkbox
  private Map<Integer, Boolean> isCheckMap = new HashMap<>();
  private LayoutInflater mInflater;
  private int mTypeId;

  public TaskListAdapter(Context context, List<Task> objects, int typeId, boolean isBat,
      int position) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mTypeId = typeId;
    this.isBat = isBat;
    // 如果长按Item，则设置长按的Item中的CheckBox为选中状态
    if (isBat && position >= 0) {
      isCheckMap.put(position, true);
    }
  }

  // Constructors
  public TaskListAdapter(Context context, List<Task> objects, int typeId) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mTypeId = typeId;
  }

  @NonNull @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_task_item, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    Task item = getItem(position);
    if (item != null && item.getSubject() != null) {

      vh.mTvSubject.setText(Html.fromHtml(item.getSubject()));
    } else {
      vh.mTvSubject.setText("");
    }

    vh.mTvStaffName.setText(item != null ? item.getCreator() : "");//提交人
    vh.mTvStatus.setVisibility(View.VISIBLE);
    switch (mTypeId) {

      case TaskListFragment.MY_TODO_LIST:
        //待办
        getProcessStatus(item != null ? item.getDelegationState() : "", vh.mTvStatus);
        vh.mTvStaffTime.setText(item != null ? item.getCreateTime() : "");//提交时间
        vh.mTvFlowLink.setText(item.getName());//流程环节名称

        vh.mCbTask.setTag(position);
        //checkbox抢占焦点使得listview的item条目不可点击
        //                vh.mCbTask.setFocusable(false);
        if (isBat) {
          vh.mCbTask.setVisibility(View.VISIBLE);
        } else {
          vh.mCbTask.setVisibility(View.GONE);
        }
        // 找到需要选中的条目
        if (isCheckMap != null && isCheckMap.containsKey(position)) {
          vh.mCbTask.setChecked(isCheckMap.get(position));
        } else {
          vh.mCbTask.setChecked(false);
        }

        vh.mCbTask.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          private int mCheckPosition;

          @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            mCheckPosition = Integer.parseInt(compoundButton.getTag().toString());
            if (isChecked) {
              // 将选中的放入hashmap中
              isCheckMap.put(mCheckPosition, true);
              LogUtil.i("isCheckMap==" + isCheckMap.toString());
            } else {
              // 取消选中的则剔除
              isCheckMap.remove(mCheckPosition);
            }
          }
        });
        break;

      case TaskListFragment.MY_DONE_LIST:
        getProcessStatus(item != null ? item.getStatus() : "", vh.mTvStatus);
        vh.mTvStaffTime.setText(item != null ? item.getCreatetime() : "");//提交时间
        break;

      case TaskListFragment.MY_REQUEST:
        getProcessStatus(item != null ? item.getStatus() : "", vh.mTvStatus);
        vh.mTvStaffTime.setText(item != null ? item.getCreatetime() : "");//提交时间
        break;
      case TaskListFragment.MY_COMPLETED:
        getProcessStatus(item != null ? item.getStatus() : "", vh.mTvStatus);
        vh.mTvStaffTime.setText(item != null ? item.getCreatetime() : "");//提交时间
        break;

      default:
        break;
    }

    return vh.rootView;
  }

  public boolean isCheckedItem(int position) {
    return isCheckMap != null && isCheckMap.containsKey(position);
  }

  private void getProcessStatus(String status, TextView view) {
    if (view == null) return;
    if (status == null) {
      view.setText("");
      return;
    }
    switch (status) {
      case "0":
        view.setText("挂起");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "1":
        view.setText("正在运行");
        view.setTextColor(getContext().getResources().getColor(R.color.green));
        break;
      case "2":
        view.setText("结束");
        view.setTextColor(getContext().getResources().getColor(R.color.green));
        break;
      case "3":
        view.setText("人工结束");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "4":
        view.setText("草稿");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "5":
        view.setText("追回");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "6":
        view.setText("驳回");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "10":
        view.setText("逻辑删除");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "bebacked":
        view.setText("驳回处理");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      case "-1":
        view.setText("待办");
        view.setTextColor(getContext().getResources().getColor(R.color.green));
        break;
      case "21":
        view.setText("转办");
        view.setTextColor(getContext().getResources().getColor(R.color.brown));
        break;
      case "15":
        view.setText("沟通意见");
        view.setTextColor(getContext().getResources().getColor(R.color.orange));
        break;
      case "26":
        view.setText("代理");
        view.setTextColor(getContext().getResources().getColor(R.color.brown));
        break;
      case "38":
        view.setText("流转意见");
        view.setTextColor(getContext().getResources().getColor(R.color.red));
        break;
      default:
        view.setText("");
        break;
    }
  }

  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView mTvStatus;
    final TextView mTvSubject;
    final CheckBox mCbTask;
    final TextView mTvStaffName;
    final TextView mTvStaffTime;
    final TextView mTvFlowLink;

    private ViewHolder(LinearLayout rootView, TextView mTvStatus, TextView mTvTitle,
        CheckBox mCbTask, TextView mTvStaffName, TextView mTvStaffTime, TextView mTvFlowLink) {
      this.rootView = rootView;
      this.mTvStatus = mTvStatus;
      this.mTvSubject = mTvTitle;
      this.mCbTask = mCbTask;
      this.mTvStaffName = mTvStaffName;
      this.mTvStaffTime = mTvStaffTime;
      this.mTvFlowLink = mTvFlowLink;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView mTvStatus = (TextView) rootView.findViewById(R.id.listview_item_tv_status);
      TextView mTvTitle = (TextView) rootView.findViewById(R.id.tv_title_taskAdapter);
      CheckBox mCbTask = (CheckBox) rootView.findViewById(R.id.cb_task);
      TextView mTvStaffName = (TextView) rootView.findViewById(R.id.tv_staffName);
      TextView mTvStaffTime = (TextView) rootView.findViewById(R.id.tv_staffTime);
      TextView mTvFlowLink = (TextView) rootView.findViewById(R.id.tv_flowLink);
      return new ViewHolder(rootView, mTvStatus, mTvTitle, mCbTask, mTvStaffName, mTvStaffTime,
          mTvFlowLink);
    }
  }
}

